package com.zzj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.entity.Tag;
import com.zzj.mapper.TagMapper;
import com.zzj.model.dto.QueryListDto;
import com.zzj.model.dto.TagsCreateDto;
import com.zzj.service.ITagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzj.utils.QueryUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 标签 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-07-17
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements ITagService {
    @Override
    public IPage<Tag> queryList(QueryListDto in) {
        QueryWrapper<Tag> queryWrapper = QueryUtil.paramsWrapper(in.getAndParams(), in.getOrParams(), in.getOrderParams());
        Page<Tag> tagPage = new Page<>(in.getPageNo(), in.getPageSize());
        return this.getBaseMapper().selectPage(tagPage, queryWrapper);
    }

    @Override
    public Tag create(TagsCreateDto in) {
        Tag tag = new Tag();
        tag.setName(in.getName());
        this.save(tag);
        return tag;
    }

    @Override
    public Tag findTag(String name) {
        return this.getOne(Wrappers.<Tag>lambdaQuery().eq(Tag::getName, name), false);
    }
}
